package cn.jingzhuan.element.demo.view

import android.content.Context
import android.content.Intent
import android.support.annotation.IdRes
import android.support.v7.app.AppCompatActivity
import android.util.Log


abstract class BaseActivity: AppCompatActivity() {

    protected abstract fun getLayoutId(): Int

    open val TAG: String = "Element"

    fun startFragment(@IdRes resId: Int,fragment: BaseFragment) {
        startFragment(resId,fragment, true)
    }

    fun startFragment(@IdRes resId: Int, fragment: BaseFragment, addToBackStack: Boolean) {
        Log.i(TAG, "startFragment")
        val transitionConfig = fragment.onFetchTransitionConfig()
        val tagName = fragment::class.java.simpleName
        val transaction = supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(transitionConfig.enter, transitionConfig.exit, transitionConfig.popenter, transitionConfig.popout)
                .replace(resId, fragment, tagName)
        if (addToBackStack) {
            transaction.addToBackStack(tagName)
        }
        transaction.commit()
    }


    companion object {
        fun openActivity(context: Context?,activityCla : Class<out BaseActivity>) {
            val intent = Intent(context, activityCla::class.java)
            context?.startActivity(intent)
        }
    }
}